package com.wit.WEB.controller;

import com.wit.WEB.entity.Order_;
import com.wit.WEB.entity.TestStand;
import com.wit.WEB.entity.User;
import com.wit.WEB.service.OrderService;
import com.wit.WEB.service.TestStandService;
import com.wit.WEB.service.UserService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: WEB
 * @description: 前端控制器
 * @author: KingTeng
 * @create: 2022-02-23 19:30
 **/
@Controller
public class ForeController {
    private UserService userService;
    private OrderService orderService;
    private TestStandService testStandService;
    @Autowired
    public void setOrderCode(OrderService orderService){this.orderService=orderService;}
    @Autowired
    public void setUserService(UserService userService){
        this.userService=userService;
    }
    @Autowired
    public void setTestStandService(TestStandService testStandService){
        this.testStandService=testStandService;
    }

    @RequestMapping("/foreHome")
    public String home(Model model){

        return "fore/home";
    }

    @RequestMapping("/foreLogout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");

        return "redirect:foreHome";
    }

    @RequestMapping("/foreUser")
    public String user(Model model){

        return "/User/listStation";
    }

    @RequestMapping("/foreAdmin")
    public String Admin(Model model){

        return "admin/listUser";
    }

    @RequestMapping("/foreStationin")
    public String Station(Model model,HttpSession session){
        User user= (User) session.getAttribute("user");
        Integer uid=user.getId();
        TestStand testStand=testStandService.findbyUid(uid);
        Integer tid=testStand.getId();
        List<Order_> orders=orderService.findallByConfirmNone(tid);
        session.setAttribute("orders", orders);
        return "station/listOrder";
    }


    @PostMapping("/foreRegister")
    public String register(String name, String pwd, Model model) {
        boolean iron=userService.isExist(name);
        if (iron){
            String msg = "用户名已经被使用,不能使用";
            model.addAttribute("msg", msg);
            return "fore/register";
        }
        userService.insertUser(HtmlUtils.htmlEscape(name), pwd);
        return "redirect:foreToLoginPage";
    }

    @RequestMapping("/forUserUpdate")
    public String forUserUpdate(Model model){

        return "/User/update";
    }

    @RequestMapping("/forOrderTest")
    public String forordertest(Model model,HttpSession session){
        User user= (User) session.getAttribute("user");
        String address=user.getAddress();

        List<TestStand> ids = testStandService.selectByCity(address);

        session.setAttribute("ids", ids);
        return "User/ordertest";
    }

    @PostMapping("/foreCreateOrder")
    public String createOrder(String tid, HttpSession session) {
        //设置订单的键uid
        Order_ order=new Order_();
        List<TestStand> ts= (List<TestStand>) session.getAttribute("ids");
        Integer ttid=ts.get(Integer.parseInt(tid)).getId();
        order.setAddress(String.valueOf(ttid));
        order.setUid(((User) session.getAttribute("user")).getId());
        //设置订单编号（订单编号由当前日期+四位随机数组成）
        order.setOrdercode(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(0, 10000));
        //设置订单状态为待确认
        order.setStatus(OrderService.waitConfirm);
        //设置创建订单的日期
        order.setCreatedate(new Date());
        orderService.addOrder(order);
//        float total;
//        String from = (String) session.getAttribute("from");
//        if ("cart".equals(from))
//            total = orderService.addOrderAndUpdateOrderItem(order, (List<OrderItem>) session.getAttribute("orderItems"));
//        else
//            total = orderService.addOrderAndOrderItem(order, (OrderItem) session.getAttribute("orderItem"));
//
//        return "redirect:foreToAlipayPage?oid="+order.getId()+"&total="+total;
        return "forOrderTest";
    }


}
